package master;

/**
 * @author chenyw
 * @date 2022/7/27 15:11
 */
public class M69mySqrt {
    public int mySqrt(int x) {
        //二分
        if (x == 0 || x == 1) {
            return x;
        }
        int left = 0;
        int right = x;
//        int ans = -1;
        while (left <= right) {
            int mid = left + (right - left) / 2;
            if ( (long)mid * mid == x) {
                return mid;
            } else if ((long)mid * mid >x){
                right = mid - 1;
            } else {
                left = mid + 1;
            }
        }
        return left * left == x ? left : left - 1;
    }
}
